﻿/***************************************************************************
Author: Armin Schmidt
Description: DB Scripts for creating function that registers
	geometry columns of views and tables in the table 'geometry_columns'.
Date: 23/04/2012
Version: 1.0
Version History:
	1.0	23/04/2012	Start of script.
***************************************************************************/

CREATE OR REPLACE FUNCTION create_geom(NAME)
RETURNS VOID
SECURITY DEFINER
LANGUAGE plpgsql AS '
DECLARE
	table_name ALIAS FOR $1;
	entry geometry_columns%ROWTYPE;
BEGIN
	SELECT * INTO entry FROM geometry_columns WHERE f_table_name = table_name;

	IF FOUND THEN
		RAISE EXCEPTION ''Geometry of ''''%'''' already registered.'',
			table_name;
	END IF;

	EXECUTE ''INSERT INTO geometry_columns(f_table_catalog,f_table_schema,f_table_name,f_geometry_column,coord_dimension,srid,"type")
		SELECT '''''''',''''public'''','''''' || table_name || '''''',''''geom_loc'''',ST_CoordDim(geom_loc),ST_SRID(geom_loc),GeometryType(geom_loc)
		FROM '' || table_name || '' LIMIT 1;'';

	RETURN;
	END
';

/*
SELECT create_geom('vvg_bores');
SELECT create_geom('vvg_bores_aq');
SELECT create_geom('vvg_bores_wl');
SELECT create_geom('vvg_bores_aq_wl');
SELECT create_geom('mv_vvg_bores');
SELECT create_geom('mv_vvg_bores_aq');
SELECT create_geom('mv_vvg_bores_wl');
SELECT create_geom('mv_vvg_bores_aq_wl');
SELECT create_geom('vvg_bores_aq_wl1');
SELECT create_geom('mv_vvg_bores_aq_wl1');
*/

SELECT create_geom('vvg_bores_wl1');
SELECT create_geom('mv_vvg_bores_wl1');
